﻿using FastExecutor.Base.Design;
using FastExecutor.Base.Util;
using FastExecutor.ORG.Entity;
using FastIOC.Annotation;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FastExecutor.ORG.DB
{
    public class DB_Task : FastTemplateDB<Frame_Task>
    {
        [AutoWired]
        public TaskUtil @TaskUtil;
        public override void Insert(Frame_Task Entity)
        {
            Entity.RowGuid = CommonUtil.CreateCommonGuid();
            Entity.AddDate = DateTime.Now;
            base.Insert(Entity);
            if (Entity.TaskStatus == 1)
            {
                @TaskUtil.StartTask(Entity);
            }
        }

        public override void Update(Newtonsoft.Json.Linq.JObject JsonEntity)
        {
            base.Update(JsonEntity);
            Frame_Task Entity = JsonConvert.DeserializeObject<Frame_Task>(JsonEntity.ToString());
            if (Entity.TaskStatus == 1)
            {
                @TaskUtil.StartTask(Entity);
            }
            else
            {
                @TaskUtil.RemoveTask(Entity);
            }
        }

        public override void Delete(string KeyGuid)
        {
            Frame_Task Entity = base.FindOne(KeyGuid);
            @TaskUtil.RemoveTask(Entity);
            base.Delete(KeyGuid);
        }

        public override void BatchDelete(List<string> KeyGuidList)
        {
            foreach (var KeyGuid in KeyGuidList)
            {
                Frame_Task Entity = base.FindOne(KeyGuid);
                @TaskUtil.RemoveTask(Entity);
            }
            base.BatchDelete(KeyGuidList);
        }
    }
}
